using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;

using Impact.Views;
using Impact.Controllers;
using Impact.Model.Objects;

namespace Impact.Web.Foundation
{
    /// <summary>
    /// base class for all user controls in the system.
    /// </summary>
    public abstract class UserControlBase : System.Web.UI.UserControl, IPageView
    {


        #region IPageView Members

        /// <summary>
        /// Gets the parameters passed on the querystring of the current request
        /// </summary>
        public NameValueCollection Parameters
        {
            get { return Request.QueryString; }
        }

        /// <summary>
        /// end the response and forward to the parameter url
        /// </summary>
        public void Redirect(string targetUrl)
        {
            Response.Redirect(targetUrl, false);
        }

        /// <summary>
        /// reloads the current page, with the same page.  used during a postback to
        /// show updated data after making changes to the data.
        /// </summary>
        public void Reload()
        {
            Response.Redirect(Request.Url.ToString(), true);
        }

        /// <summary>
        /// redirects the user back to the referring url
        /// </summary>
        public void Return()
        {
            //Redirect(ReferringUrl);
        }

        /// <summary>
        /// displays an error message on the view
        /// </summary>
        public void ShowErrorMessage(string message)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        /// <summary>
        /// the client IP address of the user
        /// </summary>
        public string UserHostAddress
        {
            get { return Request.UserHostAddress; }
        }

        /// <summary>
        /// get a string formatted as the permalink url to the item
        /// </summary>
        public string FormatUrl(object dataItem)
        {
            string url = string.Empty;

            if (dataItem is Article)
            {
                url = ContentPresenter.FormatUrl((Article)dataItem);
            }
            else if (dataItem is ArticleCategory)
            {
                url = ContentPresenter.FormatUrl((ArticleCategory)dataItem);
            }
            else if (dataItem is ArticleComment)
            {
                url = ContentPresenter.FormatUrl((ArticleComment)dataItem);
            }
            else if (dataItem is ArticleTag)
            {
                url = ContentPresenter.FormatUrl((ArticleTag)dataItem);
            }
            else if (dataItem is ImpactPage)
            {
                url = ContentPresenter.FormatUrl((ImpactPage)dataItem);
            }
            else if (dataItem is Tag)
            {
                url = ContentPresenter.FormatUrl((Tag)dataItem);
            }
            else
            {
                throw new ArgumentException(string.Format("No URL formatter for type: {0}", dataItem.GetType()));
            }

            return this.ResolveClientUrl(url);
        }


        #endregion
    }
}
